document.write('<style>\
.fu_list{width:100%;background:#ebebeb;font-size:12px;}\
.fu_list td{padding:5px;line-height:20px;background-color:#fff;}\
.fu_list table{width:100%;border:1px solid #ebebeb;}\
.fu_list thead td{background-color:#f1f1f1;}\
.fu_list tr.odd td{background-color:#fff;}\
.filelist td{background:#f8f8f8}\
.fu_list b{font-size:14px;}\
.fu_list b.r{color:red;}\
a.files{width:90px;height:30px;overflow:hidden;display:block;border:1px solid #BEBEBE;background:url(fu_btn.gif) left top no-repeat;text-decoration:none;}\
a.files:hover{background-color:#FFFFEE;background-position:0 -30px;}\
a.files input{margin-left:-350px;font-size:30px;cursor:pointer;filter:alpha(opacity=0);opacity:0;}\
a.files,a.files input{outline:none;hide-focus:expression(this.hideFocus=true);}.filelist a{color:red}\
</style>');
var fileMime = {
    mimes: { jpg: 'image/jpeg', zip: 'application/zip,application/x-zip-compressed', rar: 'application/rar,.rar', png: 'image/png', bmp: 'image/bmp', gif: 'image/gif', doc: 'application/msword', docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', xls: 'application/vnd.ms-excel', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', pdf: 'application/pdf' },
    getAccept: function (arr) { var s = ''; for (var i = 0; i < arr.length; i++) s += this.mimes[arr[i]]? ',' + this.mimes[arr[i]]:''; return s.substring(1); }
};
//method：方法名称，reset或者setExt
//fnexts：为setExt方法时，允许上传的文件名后缀，多个用英文状态下的逗号隔开
//pmax：为setExt方法时，允许上传的最大文件个数，默认为30个
$.fn.multiFiles = function (method,fnexts,pmax) {
    return this.each(function () {
        if (method == 'reset' || method == 'setExt') {
            if (method == 'setExt')  {
                var el = $(this), accept = fnexts ? fileMime.getAccept(fnexts.toLowerCase().split(',')) : '';
                el.find('font:first').html(fnexts);
                el.data('exts', fnexts ? ',' + fnexts + ',' : false).data('h5attr', ' multiple ' + (fnexts ? ' accept="' + accept + '"' : ''));
                if (pmax) {
                    el.data('max', pmax); el.find('font:eq(1)').html(pmax);
                }
                el.find('a.files input:last').attr('accept', accept);
            }
            $('a.files input:hidden', this).remove();
            $('tbody.filelist tr', this).remove();
            return true;
        }
        var el = $(this);
        if (el.data('init')) return true;
        el.data('init', true);
        var name = el.attr('name'), i = 0;
        var ext = el.attr('ext');
        var exts, h5attr = ' multiple';
        var max = parseInt(el.attr('max')) || 30;//一次最多可以添加多少个文件
        if (ext) {
            exts = ',' + ext + ',';
            h5attr += ' accept="' + fileMime.getAccept(ext.toLowerCase().split(',')) + '"';
        }
        el.append('<table border="0" cellspacing="1" class="fu_list">'
            + '<tbody><tr><td><a href="javascript:void(0);" class="files"></a></td></tr>'
            + '<tr><td colspan="2"><table border="0" cellspacing="0"><thead><tr><td>已选文件（' + (ext ? '只允许上传<font color="red">' + ext + '</font>类型文件，' : '') + '最多<font color=red>' + max + '</font>个，已经选<b class="r">0</b>个。）</td><td width="100"></td></tr></thead><tbody class="filelist"></tbody></table></td></tr></table>');
        var list = el.find('tbody.filelist');
        var a = el.find('a.files');
        var supportH5 = false;
       el.data('exts', exts).data('h5attr', h5attr).data('max',max);

        var bNum = $('b.r', this);
        function hasFile(v) { var ex = false; list.find('td.fn').each(function () { if (this.innerHTML == v) { ex = true;return false} }); return ex; }
        function allowExt(v) { var exts = el.data('exts');  if (exts) { var m = /\.([a-z\d]+)$/i.exec(v); return m && exts.indexOf(',' + m[1].toLowerCase() + ',') != -1; } return true; }
        function validate(f) {
            var num = list.find('tr').length;
            if (f.files) {
                num += f.files.length;
                for (var i = 0; i < f.files.length; i++) {
                    if (hasFile(f.files[i].name)) { alert('文件“' + f.files[i].name + '”已经存在！'); return false }
                    if (!allowExt(f.files[i].name)) { alert('只允许上传“' + ext + '”类型文件！'); return false }
                }
            }
            else {
                if (hasFile(f.value)) { alert('文件“' + f.value + '”已经存在！'); return false }
                if (!allowExt(f.value)) { alert('只允许上传“' + ext + '”类型文件！'); return false }
                num += 1;
            }
            if (num > el.data('max')) { alert('最多只能上传' + el.data('max') + '个文件！'); return false }
            return true;
        }
        function newFile() {
            h5attr = el.data('h5attr');
            bNum.html(list.find('tr').length);
            var file = $('<input type="file" name="' + name + '" id="' + name + i + '" ' + h5attr + '/>');
            file.appendTo(a);
            i++;
            file.change(function () {
                supportH5 = !!this.files;
                if (validate(this)) {
                    var odd = list.find('tr:last').hasClass('odd');
                    if (this.files) {
                        for (var i = 0; i < this.files.length; i++) addFile(this.id,this.files[i].name,odd);
                    }
                    else addFile(this.id, this.value, odd);
                    newFile();
                    this.style.display = 'none';
                }
            });
        }
        function addFile(id, value,odd) {
            var tr = $('<tr rel="' + id + '"' + (odd ? '' : ' class="odd"') + '><td class="fn">' + value + '</td><td><a href="javascript:void(0);">取消</a></td></tr>');
            list.append(tr);
            tr.find('a').click(function () {
                if (confirm('确认取消上传此' + (supportH5 ? '批次' : '') + '文件？！')) {
                    $('#' + tr.attr('rel')).remove();
                    list.find('tr[rel="' + tr.attr('rel') + '"]').remove();
                    bNum.html(list.find('tr').length);
                }
            });
        }
        newFile();
    });
}